<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04_迭代器</title>
</head>

<body>
    <script>
        //迭代器
        //1.for of
        const mingxin = ['张三', '李四', '王五']
        for (let v of mingxin) {
            console.log(v);
        }

        //2.获取Iterator
        let itr = mingxin[Symbol.iterator]()
        console.log(itr);

        //3.自定义Iterator
        const myObj = {
            name: 'lzx',
            status: ['a', 'b', 'c', 'd'],
            [Symbol.iterator]: function () {
                let index = 0
                _this = this
                return {
                    next: function () {
                        if (index < _this.status.length) {
                            let result = { value: _this.status[index], done: false }
                            index++
                            return result
                        } else {
                            return { value: undefined, done: true }
                        }
                    }
                }
            }
        }

        for (let oj of myObj) {
            console.log(oj);
        }

    </script>

</body>

</html>